package com.niit.controller.common;

import com.niit.model.Teacher;
import com.niit.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author 王铭
 * #Date 2021/6/21 - 23:07
 */
@Controller
@SessionAttributes(value = {"teacher", "date"})
public class TeacherController {
    @ModelAttribute("date")
    public Date setCurrentDate() {
        return new Date();
    }

//    @Autowired
//    public void setTeacherService(TeacherService teacherService) {
//        this.teacherService = teacherService;
//    }


    TeacherService teacherService;

    @ModelAttribute("login")
    public boolean checkLogin(@SessionAttribute("teacher") Teacher teacher) {
        if (teacher.equals(null))
            return false;
        return true;
    }

    @GetMapping("login")
    public String login(@ModelAttribute Teacher teacher) {
        return "login";
    }

    @PostMapping("login")
    public String doLogin(Teacher teacher, @ModelAttribute boolean loginSuccess, Model model) {
        Teacher teacher1 = teacherService.login(teacher);
        if (teacher1.equals(null)) {
            loginSuccess = false;
            return "login";
        }

        model.addAttribute("teacher", teacher1);
        loginSuccess = true;
        return "teacher-main";
    }


//    @GetMapping("classes")
//    public String getClasses() {
//
//        return "teacher-main";
//    }


//    @GetMapping("current")
//    public String getCurrentPage(@ModelAttribute("current") String current) {
//        if (current != null)
//            return current;
//        return "papers-uncheck";
//    }
}
